import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:state_manager/initialize_provider.dart';
import 'package:state_manager/model/user_model.dart';
import 'package:state_manager/view_model/counter_view_model.dart';
import 'package:state_manager/view_model/user_info_view_model.dart';

import 'goods_info.dart';

/// Container -> StatelessWidget -> Widget
/// Padding -> SingleChildRenderObjectWidget -> RenderObjectWidget -> Widget
/*
Padding;
  StatelessElement;
  StatefulElement;
  Padding;
* * */
// mount -> _firstBuild -> rebuild -> performRebuild -> build

main() {
  runApp(MultiProvider(
    providers: providers,
    child: DBJApp(),
  ));
}

class DBJApp extends StatelessWidget {
  const DBJApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primaryColor: Colors.orange, splashColor: Colors.transparent),
      home: DBJHomePage(),
    );
  }
}

class DBJHomePage extends StatefulWidget {
  const DBJHomePage({Key? key}) : super(key: key);

  @override
  _DBJHomePageState createState() => _DBJHomePageState();
}

class _DBJHomePageState extends State<DBJHomePage> {
  @override
  Widget build(BuildContext context) {
    print("_DBJHomePageState --- build");
    return Scaffold(
      appBar: AppBar(
        title: Text('打扮家'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            DBJCardWidget(),
            DBJContainerWidget(),
            DBJUserInfoWidget(),
            DBJManyInfoWidget(),
            DBJMovieInfo(),
            DBJGoodsInfo(),
          ],
        ),
      ),
      floatingActionButton: Selector<DBJCounterViewModel, DBJCounterViewModel>(
        selector: (cxt, viewModel1) {
          return viewModel1;
        },
        shouldRebuild: (prev, next) => false,
        builder: (cxt, viewModel2, child) {
          print('floatingActionButton --- builder');
          return FloatingActionButton(
            child: child,
            onPressed: () {
              print("++++");
              viewModel2.counter += 1;
            },
          );
        },
        child: Icon(
          Icons.add,
          color: Colors.white,
        ),
      ),
    );
  }
}

class DBJCardWidget extends StatelessWidget {
  const DBJCardWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print("DBJCardWidget --- build");
    //final counter1 = Provider.of<DBJCounterViewModel>(context).counter;
    return Card(
      color: Colors.purple,
      child: Consumer<DBJCounterViewModel>(
        builder: (cxt, viewModel, child) {
          print("Card --- Consumer --- build");
          return Text(
            '当前计数：${viewModel.counter}',
            style: TextStyle(fontSize: 20, color: Colors.white),
          );
        },
      ),
    );
  }
}

class DBJContainerWidget extends StatelessWidget {
  const DBJContainerWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print("DBJContainerWidget --- build");
    return Container(
      color: Colors.orange,
      child: Consumer<DBJCounterViewModel>(
        builder: (cxt, viewModel, child) {
          print("Container --- Consumer --- build");
          return Text(
            '当前计数：${viewModel.counter}',
            style: TextStyle(fontSize: 20, color: Colors.white),
          );
        },
      ),
    );
  }
}

/// 单模型数据的依赖
class DBJUserInfoWidget extends StatelessWidget {
  const DBJUserInfoWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<DBJUserInfoViewModel>(
      builder: (cxt, viewModel, child) {
        final name = (viewModel.userModel.name ?? "") + (viewModel.userModel.imageUrl ?? "") + (viewModel.userModel.age.toString() ?? "");
        return Text(
          name,
          style: TextStyle(fontSize: 15, color: Colors.red),
        );
      },
    );
  }
}

/// 多模型数据的依赖
class DBJManyInfoWidget extends StatelessWidget {
  const DBJManyInfoWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer2<DBJUserInfoViewModel, DBJCounterViewModel>(
      builder: (cxt, userInfoModel, counterViewModel, child) {
        String info = userInfoModel.userModel.name ?? '';
        info = info + (counterViewModel.counter.toString() ?? "");
        return Text(
          info,
          style: TextStyle(fontSize: 15, color: Colors.red),
        );
      },
    );
  }
}

class DBJMovieInfo extends StatelessWidget {
  const DBJMovieInfo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    String title = '路漫漫其修远兮，吾将上下而求索';
    final result = title.runes.map((rune) {
      return WidgetSpan(
          child: Text(
        String.fromCharCode(rune),
        style: TextStyle(
          fontSize: 16,
          color: Colors.brown,
        ),
      ));
    }).toList();

    return Text.rich(TextSpan(children: [
      WidgetSpan(
          alignment: PlaceholderAlignment.middle,
          // baseline: TextBaseline.alphabetic,
          child: Icon(
            Icons.people,
            color: Colors.red,
            size: 40,
          )),
      ...result,
    ]));
  }
}
